package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Supplier;
import cn.wolfcode.wms.mapper.SupplierMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    public void deleteById(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Supplier supplier) {
        if (supplier.getId() != null) {
            supplierMapper.updateByPrimaryKey(supplier);
        } else {
            supplierMapper.insert(supplier);
        }
    }

    public Supplier getById(Long id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    public List<Supplier> selectAll() {
        return supplierMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = supplierMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Supplier> data = supplierMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    @Override
    public void deleteall(Long[] ids) {
        if (ids!=null){
            for (Long id : ids) {
                supplierMapper.deleteByPrimaryKey(id);
            }
        }
    }
}
